//
//  ICMBalanceBasicInfoView.m
//  ICrEAM
//
//  Created by kuroro on 16/11/25.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMBalanceBasicInfoView.h"
#import "RepairOrderTypeRespose.h"


@interface ICMBalanceBasicInfoView()<ICMTitleTextFeildViewDelegate>
@property (nonatomic, strong) TPKeyboardAvoidingScrollView *bigScrollView;

@end

#define xDistanceA                  ActualWidth(30)
#define xDistanceB                  ActualWidth(50)
#define yDistance                   (ScreenHeight-240-6*TitleTFViewHeight)/7-5
#define TitleTFViewHeight           ActualHeight(60)
#define TitleTViewHeight            ActualHeight(73)

static  int verificationTime = 60;

@implementation ICMBalanceBasicInfoView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        self.frame = frame;
        self.bigScrollView = [[TPKeyboardAvoidingScrollView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame))];
        self.bigScrollView.backgroundColor = [UIColor clearColor];
        self.isVisit = NO;
        self.backgroundColor = BackgroundColor_White;
        //第一行
        ICMTitleTextFeildView *vinNumberView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(ActualWidth(20), yDistance, ActualWidth(220), TitleTFViewHeight) title:@"VIN"];
        [self.bigScrollView addSubview:vinNumberView];
        
        
        ICMTitleTextFeildView *plateNumberView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(vinNumberView.rightEdge+xDistanceA, vinNumberView.y, ActualWidth(220), TitleTFViewHeight) title:@"车牌号"];
        [self.bigScrollView addSubview:plateNumberView];
        
        ICMTitleTextFeildView *modelCodeView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(plateNumberView.rightEdge+xDistanceB, plateNumberView.y, ActualWidth(470), TitleTFViewHeight) title:@"车型"];
        [self.bigScrollView addSubview:modelCodeView];
        
        self.vinNumberView = vinNumberView;
        self.plateNumberView = plateNumberView;
        self.modelCodeView = modelCodeView;
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
        lineView.backgroundColor = SepColor_gray;
        [self.bigScrollView addSubview:lineView];

        //第二行
        ICMTitleTextFeildView *salesDateView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(vinNumberView.x, vinNumberView.bottomEdge+yDistance, ActualWidth(220), TitleTFViewHeight) title:@"购车发票日期"];
        [self.bigScrollView addSubview:salesDateView];
        
        ICMTitleTextFeildView *useKindView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(plateNumberView.x, salesDateView.y, ActualWidth(220), TitleTFViewHeight) title:@"使用性质"];
        NSArray *useKindArray = [ICrEAMTool getParameterArrayWithCode:ICMEnumIntToString(ICMEnumUseKindPre)];
        
        useKindView.clickDataArray = [[ICrEAMTool getParameterTitleArrayWithArray:useKindArray] mutableCopy];
        [self.bigScrollView addSubview:useKindView];
        
        ICMTitleTextFeildView *accountsTypeView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(modelCodeView.x, salesDateView.y, ActualWidth(220), TitleTFViewHeight) title:@"结算方式" addTarget:nil selector:nil];
        NSArray *accountsTypeArray = [ICrEAMTool getParameterArrayWithCode:ICMEnumIntToString(ICMEnumBalanceTypePre)];
        accountsTypeView.clickDataArray = [[ICrEAMTool getParameterTitleArrayWithArray:accountsTypeArray] mutableCopy];
        [self.bigScrollView addSubview:accountsTypeView];
        
        ICMTitleTextFeildView *levelView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(accountsTypeView.rightEdge+xDistanceA, accountsTypeView.y, ActualWidth(220), TitleTFViewHeight) title:@"行管类型" addTarget:nil selector:nil];
        self.levelView = levelView;
        self.levelView.delegate = self;
        [self getLevelViewData];
        [self.bigScrollView addSubview:levelView];
      
        
        self.salesDateView = salesDateView;
        self.useKindView = useKindView;
        self.accountsTypeView = accountsTypeView;
        self.accountsTypeView.delegate = self;
       
        
        
        //第三行
        ICMTitleTextFeildView *remarkView = [ICMTitleTextFeildView createEditableICMTitleTextFeildViewWithFrame:CGRectMake(salesDateView.x, salesDateView.bottomEdge+yDistance, levelView.rightEdge - salesDateView.x, TitleTViewHeight) title:@"备注"];
        [self.bigScrollView addSubview:remarkView];
        self.remarkView.delegate = self;
        self.remarkView = remarkView;
        
        UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(0, remarkView.bottomEdge+ActualHeight(14), ScreenWidth, 1)];//第三行底部分割线
        line1.backgroundColor = SepColor_gray;
        [self.bigScrollView addSubview:line1];
        
        UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(modelCodeView.x-(xDistanceB/2), modelCodeView.y, 1, useKindView.bottomEdge-modelCodeView.y +10)];//中间分割线
        line2.backgroundColor = SepColor_gray;
        [self.bigScrollView addSubview:line2];
        
        
        //第四行
        
        ICMTitleTextFeildView *proxyNOView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(ActualWidth(20), line1.bottomEdge+ActualHeight(15), ActualWidth(220), TitleTFViewHeight) title:@"委托书号"];
        [self.bigScrollView addSubview:proxyNOView];
        
        
        ICMTitleTextFeildView *createDateView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(vinNumberView.rightEdge+xDistanceA, proxyNOView.y, ActualWidth(220), TitleTFViewHeight) title:@"开单日期"];
        [self.bigScrollView addSubview:createDateView];
        
        ICMTitleTextFeildView *ownerNameView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(createDateView.rightEdge+xDistanceB, proxyNOView.y, ActualWidth(470), TitleTFViewHeight) title:@"车主姓名"];
        [self.bigScrollView addSubview:ownerNameView];
        
        self.proxyNOView = proxyNOView;
        self.createDateView = createDateView;
        self.ownerNameView = ownerNameView;
        
        //第五行
        ICMTitleTextFeildView *repairTypeView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(proxyNOView.x, proxyNOView.bottomEdge+yDistance, ActualWidth(470), TitleTFViewHeight) title:@"维修类型"];
        [self.bigScrollView addSubview:repairTypeView];
        
        ICMTitleTextFeildView *deliverNameView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(accountsTypeView.x, repairTypeView.y, ActualWidth(220), TitleTFViewHeight) title:@"送修人姓名"];
        [self.bigScrollView addSubview:deliverNameView];
        
        ICMTitleTextFeildView *deliverMobileView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(levelView.x, repairTypeView.y, ActualWidth(220), TitleTFViewHeight) title:@"送修人手机"];
        [self.bigScrollView addSubview:deliverMobileView];
        
        self.repairTypeView = repairTypeView;
        self.deliverNameView = deliverNameView;
        self.deliverMobileView = deliverMobileView;
        
        //第六行
        
        ICMTitleTextFeildView *visitDateView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(repairTypeView.x, repairTypeView.bottomEdge+yDistance, ActualWidth(220), TitleTFViewHeight) title:@"回访时间" addTarget:nil selector:nil];
        NSArray *visitDateArray = [ICrEAMTool getParameterArrayWithCode:ICMEnumIntToString(ICMEnumTraceTimePre)];
        visitDateView.clickDataArray = [[ICrEAMTool getParameterTitleArrayWithArray:visitDateArray] mutableCopy];
        [self.bigScrollView addSubview:visitDateView];
        
        ICMTitleTextFeildView *visitTypeView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(createDateView.x, repairTypeView.bottomEdge+yDistance, ActualWidth(220), TitleTFViewHeight) title:@"回访方式" addTarget:nil selector:nil];
        NSArray *visitTypeArray = [ICrEAMTool getParameterArrayWithCode:ICMEnumIntToString(ICMEnumRevisitWayPre)];
        visitTypeView.clickDataArray = [[ICrEAMTool getParameterTitleArrayWithArray:visitTypeArray] mutableCopy];
        [self.bigScrollView addSubview:visitTypeView];
        
        ICMTitleTextFeildView *visitReasonView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(deliverNameView.x, repairTypeView.bottomEdge+yDistance, ActualWidth(220), TitleTFViewHeight) title:@"不回访原因" addTarget:nil selector:nil];
        visitReasonView.borderView.backgroundColor = TextFeildBackgroundColor_Gray;
        visitReasonView.userInteractionEnabled = NO;
        NSArray *visitReasonArray = [ICrEAMTool getParameterArrayWithCode:ICMEnumIntToString(ICMEnumNoTracePre)];
        visitReasonView.clickDataArray = [[ICrEAMTool getParameterTitleArrayWithArray:visitReasonArray] mutableCopy];
        [self.bigScrollView addSubview:visitReasonView];
        
        ICMTitleTextFeildView *oldPartView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(deliverMobileView.x, repairTypeView.bottomEdge+yDistance, ActualWidth(220), TitleTFViewHeight) title:@"旧件处理" addTarget:nil selector:nil];
        NSArray *oldPartArray = [ICrEAMTool getParameterArrayWithCode:ICMEnumIntToString(ICMEnumOldPartProcessePre)];
        oldPartView.clickDataArray = [[ICrEAMTool getParameterTitleArrayWithArray:oldPartArray] mutableCopy];
        [self.bigScrollView addSubview:oldPartView];
        
        self.visitDateView = visitDateView;
        self.visitDateView.delegate = self;
        self.visitTypeView = visitTypeView;
        self.visitTypeView.delegate = self;
        self.visitReasonView = visitReasonView;
        self.visitReasonView.delegate = self;
        self.oldPartView = oldPartView;
        self.oldPartView.delegate = self;
        
        UIButton *carInfoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        carInfoBtn.frame = CGRectMake(plateNumberView.rightEdge-ActualWidth(90), plateNumberView.y, ActualWidth(23), ActualWidth(23));
        [carInfoBtn setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:[UIImage imageNamed:Appointment_CarInfo] selectedBackImage:nil actionSEL:@selector(clickCarInfoBtn) target:self];
        [self.bigScrollView addSubview:carInfoBtn];
        
        UIButton *monitorBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        monitorBtn.frame = CGRectMake(plateNumberView.rightEdge-ActualWidth(30), plateNumberView.y, ActualWidth(23), ActualWidth(23));
        [monitorBtn setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:[UIImage imageNamed:Appointment_CarMonitor] selectedBackImage:nil actionSEL:@selector(clickMonitorBtn) target:self];
        [self.bigScrollView addSubview:monitorBtn];
        
        UIButton *carOwnerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        carOwnerBtn.frame = CGRectMake(ownerNameView.rightEdge - ActualWidth(30), ownerNameView.y, ActualWidth(23), ActualWidth(23));
        [carOwnerBtn setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:[UIImage imageNamed:Appointment_CarOwner] selectedBackImage:nil actionSEL:@selector(clickCarOwnerBtn) target:self];
        [self.bigScrollView addSubview:carOwnerBtn];
        
        UIButton *yesBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        yesBtn.frame = CGRectMake(visitDateView.rightEdge - ActualHeight(30), visitDateView.y, ActualWidth(23), ActualWidth(23));
        [yesBtn setImage:[UIImage imageNamed:@"balance_unselected"] forState:UIControlStateNormal];
        [yesBtn setImage:[UIImage imageNamed:Balance_Confirm] forState:UIControlStateSelected];
        [yesBtn addTarget:self action:@selector(clickYesBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self clickYesBtn:yesBtn];
        [self.bigScrollView addSubview:yesBtn];
        
        self.bigScrollView.contentSize = CGSizeMake(ScreenWidth, oldPartView.bottomEdge);
        [self addSubview:self.bigScrollView];
    }
    return self;
}

+ (instancetype)createICMBalanceBasicInfoViewWithFrame:(CGRect)frame{
    return [[ICMBalanceBasicInfoView alloc] initWithFrame:frame];
}

#pragma mark -- 点击车辆信息

-(void)clickCarInfoBtn{
    if ([self.delegate respondsToSelector:@selector(clickCarInfoBtn)]) {
        [self.delegate clickCarInfoBtn];
    }
}

#pragma mark -- 点击监控信息
-(void)clickMonitorBtn{
    if ([self.delegate respondsToSelector:@selector(clickMonitorBtn)]) {
        [self.delegate clickMonitorBtn];
    }
}

#pragma mark -- 点击车主信息
-(void)clickCarOwnerBtn{
    if ([self.delegate respondsToSelector:@selector(clickCarOwnerBtn)]) {
        [self.delegate clickCarOwnerBtn];
    }
}

#pragma mark -- 选择按钮
-(void)clickYesBtn:(UIButton *)sender{
    sender.selected=!sender.selected;
    self.isVisit = !self.isVisit;
    if (sender.selected) {
        self.visitReasonView.borderView.backgroundColor = TextFeildBackgroundColor_Gray;
        self.visitReasonView.userInteractionEnabled = NO;
        self.visitReasonView.contentText = @"";
        self.visitDateView.borderView.backgroundColor = TextFeildBackgroundColor_OffWhite;
        self.visitDateView.userInteractionEnabled = YES;
        self.visitDateView.contentText = @"";
        self.visitTypeView.borderView.backgroundColor = TextFeildBackgroundColor_OffWhite;
        self.visitTypeView.contentText = @"";
        self.visitTypeView.userInteractionEnabled = YES;
        
    }else{
        self.visitDateView.borderView.backgroundColor = TextFeildBackgroundColor_Gray;
        self.visitDateView.userInteractionEnabled = NO;
        self.visitDateView.contentText = @"";
        self.visitTypeView.borderView.backgroundColor = TextFeildBackgroundColor_Gray;
        self.visitTypeView.userInteractionEnabled = NO;
        self.visitTypeView.contentText = @"";
        self.visitReasonView.borderView.backgroundColor = TextFeildBackgroundColor_OffWhite;
        self.visitReasonView.contentText = @"";
        self.visitReasonView.userInteractionEnabled = YES;
    }
}

#pragma mark -- 行管类型获取

- (void)getLevelViewData{
    NSArray *levelDataArray = [ICrEAMTool queryRepairOrderTypes];
    NSMutableArray *provinceNameArray = [[NSMutableArray alloc] init];
    NSMutableArray *provinceNoArray = [[NSMutableArray alloc] init];
    for(int i = 0;i<levelDataArray.count;i++)
    {
        RepairOrderType *provinceData = levelDataArray[i];
        [provinceNameArray addObject:provinceData.REPAIR_ORDER_TYPE_NAME];
        [provinceNoArray addObject:provinceData.REPAIR_ORDER_TYPE_CODE];
    }
    self.levelView.clickDataArray = provinceNameArray;
    self.levelView.clickDataNoArray = provinceNoArray;
}

#pragma mark -- 设置页面

- (void)setBasicInfoViewWithModel:(ICMBalanceBasicInfoModel *)model{
    self.vinNumberView.contentText = model.VIN;
    self.plateNumberView.contentText = model.LICENSE;
    self.modelCodeView.contentText = [NSString stringWithFormat:@"%@|%@",model.MODEL_CODE,model.MODEL_NAME];
    self.HONEST_BUSINESS = model.HONEST_BUSINESS;
    self.salesDateView.contentText = StrIsZero_gdm(model.SALES_DATE)?@"":[ICrEAMTool getDateWithTimeInterval:model.SALES_DATE];
    self.useKindView.contentText = model.USE_KIND_DESC;
    self.oldPartView.contentText = model.OLD_PART_PROCESS_DESC;
    self.remarkView.contentText = model.REMARK;
    self.proxyNOView.contentText = model.RO_NO;
    self.createDateView.contentText = StrIsZero_gdm(model.RO_CREATE_DATE)?@"":[ICrEAMTool getDateWithTimeInterval:model.RO_CREATE_DATE];
    self.ownerNameView.contentText = model.OWNER_NAME;
    self.repairTypeView.contentText = model.RO_REPAIR_TYPE_NAME;
    self.deliverNameView.contentText = model.DELIVERER;
    self.deliverMobileView.contentText = model.DELIVERER_MOBILE;
}

#pragma 页面值变化
//值变化调用此方法
- (void)iCMTitleTextFeildViewValueChanged
{
    self.hasViewValueChanged = YES;
}

//值变化调用此方法
- (void)iCMTitleTextViewValueChanged
{
    self.hasViewValueChanged = YES;
}

- (void)setHasViewValueChanged:(BOOL)hasViewValueChanged
{
    _hasViewValueChanged = hasViewValueChanged;
    if(_hasViewValueChanged)
    {
        //发送变化通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"BalanceViewValueChanged" object:self];
    }
}


@end
